#!/bin/bash

export JAVA_OPTS="-Xmx1024m -Xms512m"
export JOY_SPORT_APP="joy-sport"
export JAR_NAME="/home/admin/application/target/joy-sport-0.0.1-SNAPSHOT.jar"
export FILE_PATH="/var/log/joy-sport.log"

# 获取 joy-sport.jar 的进程 ID
pid=$(ps -ef | grep $JOY_SPORT_APP | grep -v grep | awk '{print $2}')

echo "" >> $FILE_PATH
echo "当前时间 : $(date +'%Y-%m-%d %H:%M:%S')" >> $FILE_PATH

# 如果有进程 ID，即服务正在运行，则 kill 掉该进程
if [ -n "$pid" ]; then
    echo "停止joy-sport服务，service with PID $pid..." >> $FILE_PATH
    kill -9 $pid
else
    echo "未找到上次的joy-sport服务" >> $FILE_PATH
fi

# 启动服务
echo "Starting joy-sport service..." >> $FILE_PATH
nohup /usr/local/jdk-17.0.12/bin/java -jar $JAVA_OPTS $JAR_NAME >> $FILE_PATH 2>&1 &

echo "Joy-sport service started." >> $FILE_PATH
echo "" >> $FILE_PATH